﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.Service
{
    /// <summary>
    /// CustHandler 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class CustHandler : IHttpHandler
    {
        ICustomerBLL custBLL = IocContainer.Instance.Resolve<ICustomerBLL>();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string result = "";
            try
            {
                // 0表示新增，其它表示修改
                string custIdArr = context.Request["CustIdParas"];
                string custArr = context.Request["CustParas"];
                string contactArr = context.Request["ContactParas"];
                string addressArr = context.Request["AddressParas"];
                if (!string.IsNullOrEmpty(custArr))
                {
                    string[] custs = custArr.Split(',');
                    Customer custModel = new Customer();
                    custModel.FNumber = custs[0];
                    custModel.FName = custs[1];
                    custModel.FJB = Int32.Parse(custs[2]);
                    custModel.FSalesId = Int32.Parse(custs[3]);
                    custModel.FQH = custs[4];
                    custModel.FTel = custs[5];
                    custModel.FMobil = custs[6];
                    custModel.FDree = custs[7];
                    custModel.CustBrand = custs[8];
                    custModel.CustMarket = custs[9];
                    custModel.Products = custs[10];
                    custModel.Channels = custs[11];
                    custModel.WebAddress = custs[12];
                    int custId = 0;
                    bool temp = custBLL.Exists(custModel.FNumber, out custId);
                    List<CustContact> listContact = new List<CustContact>();
                    List<CustDree> listAdd = new List<CustDree>();
                    if (!string.IsNullOrEmpty(contactArr))
                    {
                        // 联系人
                        string[] contactRows = contactArr.Split(';');
                        foreach (string row in contactRows)
                        {
                            string[] contactCols = row.Split(',');
                            CustContact contactModel = new CustContact();
                            contactModel.FName = contactCols[0];
                            contactModel.FEmail = contactCols[1];
                            contactModel.FTel = contactCols[2];
                            contactModel.FMobile = contactCols[3];
                            contactModel.FIsD = contactCols[4].ToLower() == "true" ? true : false;
                            contactModel.FDree = contactCols[5];
                            contactModel.FHobby = contactCols[6];
                            contactModel.FNote = contactCols[7];
                            listContact.Add(contactModel);
                        }
                    }
                    if (!string.IsNullOrEmpty(addressArr))
                    {
                        // 收货地址
                        string[] addressRows = addressArr.Split(';');
                        foreach (string addRow in addressRows)
                        {
                            string[] addCols = addRow.Split(',');
                            CustDree dreeModel = new CustDree();
                            dreeModel.FName = addCols[0];
                            dreeModel.FTel = addCols[1];
                            dreeModel.FMobile = addCols[2];
                            dreeModel.FNumber = addCols[3] == "" ? 0 : Int32.Parse(addCols[3]);
                            dreeModel.FDreeName = addCols[4];
                            listAdd.Add(dreeModel);
                        }
                    }
                    if (custIdArr == "0")//新增
                    {
                        if (!temp)
                        {
                            custBLL.Add(custModel, listContact, listAdd);
                            result = "1";
                        }
                        else
                            result = "2";
                    }
                    else // 修改
                    {
                        if (temp == false || (temp == true && Int32.Parse(custIdArr) == custId))
                        {
                            custModel.FItemId = custId;
                            custBLL.Update(custModel, listContact, listAdd);
                            result = "1";
                        }
                        else
                            result = "2";
                    }
                }
            }
            catch (Exception)
            {
                // 程序错误
                result = "error";
            }
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}